package com.itheima.mm.controller;

import com.itheima.mm.base.BaseController;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.Question;
import com.itheima.mm.service.QuestionService;
import com.itheima.mm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.itheima.mm.common.GlobalConst.SESSION_KEY_USER;
@CrossOrigin
@RestController
@RequestMapping("/question")
public class QuestionController extends BaseController {
    @Autowired
    private QuestionService questionService;

    @PostMapping("/findListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean){
        List<Question> questionList= questionService.findListByPage(queryPageBean);
        /*获取所有题目的问题*/
        Long total = questionService.findTotal(queryPageBean);
        return new Result(true,"获取题目列表成功",new PageResult(total,questionList));
    }

    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Question question){
        if(question.getId()==0){
            question.setUserId(1);
            question.setUserId(1);
            question.setIsClassic(0);
            question.setCreateDate(DateUtils.parseDate2String(new Date()));
        }

        questionService.add(question);
        return new Result(true,"提交成");
    }

    @PostMapping("/findClassicListByPage")
    public Result findClassicListByPage(@RequestBody QueryPageBean queryPageBean){
        if(queryPageBean.getQueryParams()==null){
            HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
            stringIntegerHashMap.put("isClassic",1);
            queryPageBean.setQueryParams(stringIntegerHashMap);
        }else {
            queryPageBean.getQueryParams().put("isClassic",1);
        }

        List<Question> questionList= questionService.findClassicListByPage(queryPageBean);
        /*获取所有题目的问题*/
        Long total = questionService.findTotal(queryPageBean);
        return new Result(true,"获取题目列表成功",new PageResult(total,questionList));
    }

    @GetMapping("/findById")
    public Result findById(@RequestParam("questionId") Integer id){
        Question question= questionService.findById(id);
        /*获取所有题目的问题*/
        return new Result(true,"获取题目列表成功",question);
    }
    @PostMapping("/delete")

    public Result delete(@RequestParam("questionId") Integer questionId){
        System.out.println("666");

        return new Result(true,"删除成功");
    }


    @GetMapping("/updateClassic")

    public Result updateClassic(@RequestParam("questionId") Integer questionId){
         if (questionService.updateClassic(questionId)) {
             return new Result(true,"已经更改成功");
        }
        return new Result(false,"已经是精选了");
    }
}
